﻿using EvaluationSystem.Utils;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace EvaluationSystem.Filters;

public class AuthorizationFilter : ActionFilterAttribute
{
    private readonly IList<string?> _whiteList = new List<string?>();

    public AuthorizationFilter()
    {
        _whiteList.Add("Login");
        _whiteList.Add("*");
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var descriptor = context.ActionDescriptor;
        var action = descriptor.RouteValues.FirstOrDefault().Value;

        if (_whiteList.Contains(action))
        {
            base.OnActionExecuting(context);
        }
        else
        {
            if (!SessionToken.GetUserToken(context.HttpContext, out var value))
                context.Result = new RedirectToActionResult("Login", "Home", null);
        }
    }
}